package com.example.gsq.news.base;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

import com.example.gsq.news.util.ActivityCollector;
import com.example.gsq.news.util.C;

import java.util.HashMap;

public class BaseActivity extends AppCompatActivity {

    private BaseTaskPool mTaskPool;
    private BaseHandler mHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityCollector.addActivity(this);
        mHandler = new BaseHandler(this);
        mTaskPool = new BaseTaskPool(mHandler);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityCollector.removeActivity(this);
    }

    public void forward(Class<?> cls) {
        Intent intent = new Intent();
        intent.setClass(this, cls);
        startActivity(intent);
    }

    public void forward(Class<?> cls, String data) {
        Intent intent = new Intent();
        intent.setClass(this, cls);
        intent.putExtra("data", data);
        startActivity(intent);
    }

    public void forward(Class<?> cls, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(this, cls);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    public void doAsyncTask(int taskId, String urlString) {
        doAsyncTask(taskId, C.TaskType.TASK_PULL_REFRESH, urlString);
    }

    public void doAsyncTask(int taskId, int taskType, String urlString) {
        mTaskPool.addTask(taskId, taskType, urlString);
    }

    public void doAsyncTask(int taskId, String urlString, HashMap<String, String> param) {
        mTaskPool.addTask(taskId, C.TaskType.TASK_PULL_REFRESH, urlString, param);
    }

    public void onTaskComplete(int taskId, int taskType, String responseString) {

    }

    public void onTaskComplete(int taskId, int taskType, Bitmap bitmap, String imageUrl) {

    }

    public void onTaskError(int taskId) {

    }

    public void toast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }


    public static boolean isNetworkAvailable(final Context context) {

        boolean hasWifiCon = false;
        boolean hasMobileCon = false;

        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(context.CONNECTIVITY_SERVICE);
        NetworkInfo[] netInfos = cm.getAllNetworkInfo();
        for (NetworkInfo net : netInfos) {

            String type = net.getTypeName();
            if (type.equalsIgnoreCase("WIFI")) {
                if (net.isConnected()) {
                    hasWifiCon = true;
                }
            }

            if (type.equalsIgnoreCase("MOBILE")) {
                if (net.isConnected()) {
                    hasMobileCon = true;
                }
            }
        }
        return hasWifiCon || hasMobileCon;
    }

}
